using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
using DalatCooking.Core.Helper;
using System.Collections;
namespace DalatCooking.Core
{
    class PersonAccessor : IPersonRepository
    {
        public override bool Insert(Person entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@PersonId", "Person_Insert", entity.PersonId, entity.UserName, MyEncode.Encode(entity.Password), entity.Email, entity.IsActive);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Person entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_Update",entity.PersonId, entity.UserName, MyEncode.Encode(entity.Password), entity.Email, entity.IsActive);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Person_Delete", id) > 0;
        }
        public override bool Delete(Person entity)
        {
            return Delete(entity.PersonId);
        }

        public override Person Login(string userName, string password)
        {
            Person p = CBO.FillObject<Person>(IDataProvider.Instance.ExecuteReader("Person_Login", userName, MyEncode.Encode(password)));

            if (p != null)
            {
                p.Profile = CBO.FillObject<Profile>(IDataProvider.Instance.ExecuteReader("Profile_FindByPersonId", p.PersonId));
                p.Roles = PersonManager.FindAllPersonRoles(p.PersonId);
            }
            return p;
        }
        public override Person LoginEmail(string email, string password)
        {
            Person p = CBO.FillObject<Person>(IDataProvider.Instance.ExecuteReader("Person_LoginEmail", email, MyEncode.Encode(password)));
            if (p != null)
            {
                p.Profile = CBO.FillObject<Profile>(IDataProvider.Instance.ExecuteReader("Profile_FindByPersonId", p.PersonId));
                p.Roles = PersonManager.FindAllPersonRoles(p.PersonId);
            }
            return p;
        }
       
        public override bool ChangePassword(Person entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_ChangePassword", entity.UserName, entity.Email, MyEncode.Encode(entity.Password), MyEncode.Encode(entity.PasswordNew));
            return rs>0;
        }

        public override bool ResetPassword(Person entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_RecoveryPassword", entity.Email, MyEncode.Encode(entity.PasswordNew));
            return rs>0;            
        }

        public override List<Person> FindAll()
        {
            List<Person> temp = new List<Person>();
            List<Person> list = CBO.FillCollection<Person>(IDataProvider.Instance.ExecuteReader("Person_FindAll"));
            foreach (Person p in list)
            {
                if (p != null)
                    p.Profile = ProfileManager.FindByPersonId(p.PersonId);
                temp.Add(p);
            }
            return temp;
        }
        public override Person FindById(int id)
        {
            Person p = CBO.FillObject<Person>(IDataProvider.Instance.ExecuteReader("Person_FindById", id));
            if (p != null)
                p.Profile = ProfileManager.FindByPersonId(p.PersonId);
            return p;
        }
        public override Person Single(int id)
        {
            return FindById(id);
        }
        public override Person FindByUserName(string username)
        {
            Person p = CBO.FillObject<Person>(IDataProvider.Instance.ExecuteReader("Person_FindByUserName", username));
            if (p != null)
                p.Profile = ProfileManager.FindByPersonId(p.PersonId);
            return p;
        }
        public override Person FindByEmail(string email)
        {
            Person p = CBO.FillObject<Person>(IDataProvider.Instance.ExecuteReader("Person_FindByEmail", email));
            if (p != null)
                p.Profile = ProfileManager.FindByPersonId(p.PersonId);
            return p;
        }
        public override List<Person> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Person_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Person>(reader);
        }
        public override List<Person> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
            var query = from c in FindAll()
                        select c;
            totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Person_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Person_RestoreAll");
            return rs > 0;
        }
        public override System.Collections.ArrayList FindAllPersonRoles(int personID)
        {          
            ArrayList roles = new ArrayList();
            IDataReader reader = IDataProvider.Instance.ExecuteReader("Person_GetRoles", personID);
            while (reader.Read())
                roles.Add(reader.GetString(1));
            return roles;
        }
        public override List<Role> FindAllRolesById(int personID)
        {
            return CBO.FillCollection<Role>(IDataProvider.Instance.ExecuteReader("Person_GetRoles", personID));
        }
        public override bool Person_AddRole(int personId, int roleId)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Person_AddRole", personId, roleId)>0;
        }
        public override bool Person_RemoveRole(int personId, int roleId)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Person_RemoveRole", personId, roleId) > 0;
        }
        public override List<Person> FindAllMembers(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Person_Member_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Person>(reader);
        }
    }
}